/**
 * 
 */
package com.ruyipu.forum.base;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.ruyipu.forum.R;
import com.ruyipu.forum.activity.CategoryActivity;
import com.ruyipu.forum.activity.DiscussionActivity;
import com.ruyipu.forum.item.Category;
import com.ruyipu.forum.item.Discussion;

/**
 * @author donald
 * 
 */
public class GlobalUtil {
	
	private GlobalUtil(){}

	public static void requestLogin(Context context) {
		requestLogin(context, "");
	}

	public static void requestLogin(Context context, String target) {
		// Intent intent = new Intent();
		// if (isLogined(context)) {
		// intent.setClassName(context, addPackagePrefix(context, target));
		// } else {
		// intent.putExtra("target", target);
		// //intent.setClass(context, LoginActivity.class);
		// }
		// context.startActivity(intent);
	}

	public static String addPackagePrefix(Context context, String s) {
		return context.getPackageName() + "." + s;
	}

	public static void showLongToast(Context context, String msg) {
		Toast.makeText(context, msg, Toast.LENGTH_LONG).show();
	}

	public static void showShortToast(Context context, String msg) {
		Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
	}

	public static Date cookieStringToDate(String d) {
		// "Fri Jul 19 08:53:47 格林尼治标准时间+0800 2013";
		String[] Months = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",
				"Aug", "Sept", "Oct", "Nov", "Dec" };
		String sShortMonth = d.substring(4, 7);
		int i = 0;
		for (; i < 12; i++) {
			if (Months[i].equals(sShortMonth)) {
				break;
			}
		}
		String sYear = d.substring(34, 38);
		int iMonth = i + 1;
		String sDay = d.substring(8, 10);
		String sTime = d.substring(11, 19);
		// 2013 8 19 08:53:47
		SimpleDateFormat df = new SimpleDateFormat("yyyy-M-dd-kk:mm:ss");
		String sDate = sYear + "-" + iMonth + "-" + sDay + "-" + sTime;
		Date date = null;
		try {
			date = df.parse(sDate);
		} catch (ParseException e) {
			e.printStackTrace();
			Log.e("FM", e.toString());
		}
		return date;
	}

	public static void viewCategory(Context context, Category item) {
		Intent intent = new Intent();
		Bundle bundle = new Bundle();
		bundle.putSerializable(Category.TAG, item);
		intent.putExtras(bundle);
		intent.setClass(context, CategoryActivity.class);
		context.startActivity(intent);
	}
	
	public static void viewDiscussion(Activity activity, Discussion discussion) {
		Intent intent = new Intent();
		intent.putExtra(Discussion.TAG, discussion);
		intent.setClassName(activity, DiscussionActivity.NAME);
		GlobalUtil.startSonActivity(activity, intent);
	}
	
	public static void startActivity(Activity activity, Intent intent, int enterAnim, int exitAnim) {
		activity.startActivity(intent);
		activity.overridePendingTransition(enterAnim, exitAnim);
	}
	
	public static void startSonActivity(Activity activity, Intent intent) {
		if(null == intent) {
			intent = new Intent();
		}
		intent.putExtra(Constants.ACTIVITY_RELATION, Constants.RELATION_SON);
		startActivity(activity, intent, R.anim.up_enter, R.anim.up_exit);
	}
	
	public static void startBrotherActivity(Activity activity, Intent intent) {
		if(null == intent){
			intent = new Intent();
		}
		intent.putExtra(Constants.ACTIVITY_RELATION, Constants.RELATION_BROTHER);
		startActivity(activity, intent, R.anim.left_enter, R.anim.left_exit);
	}
	
	public static String fixUserPhotoUrl(String url) {
		if(!url.startsWith(Constants.SITE_URL) || !url.startsWith("http") || !url.startsWith("https")) {
			url = Constants.URL_UPLOAD + url;
		}
		return url;
	}

}
